package com.IC.base.controller;

import IC.common.core.domain.R;
import com.IC.base.service.AccessControlRecordService;
import com.IC.domain.po.AccessControlRecordPO;
import com.IC.domain.vo.AccessControlRecordVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import java.util.List;

@RestController
@RequestMapping("/accessControlRecord")
public class AccessControlRecordController {

    @Autowired
    private AccessControlRecordService accessControlRecordService;

    @Autowired
    private RedisTemplate<String, Object> baseRedisTemplate;



    @GetMapping("/list")
    @Transactional
    public R<List<AccessControlRecordVO>> list() {
        List<AccessControlRecordVO> accessControlRecordPOS = accessControlRecordService.find();
        return R.ok(accessControlRecordPOS);
    }

    @PostMapping("/add")
    public R add(@RequestBody AccessControlRecordPO accessControlRecord) {
        accessControlRecordService.add(accessControlRecord);
        refreshCache();
        return R.ok();
    }

    private void cacheAllData() {
        List<AccessControlRecordPO> all = accessControlRecordService.list();
        baseRedisTemplate.opsForValue().set("AccessControlRecord", all);
    }

    /**
     * author 邹广烨
     * */
    @PostMapping("/addVehiclePassageRecord")
    public R addVehiclePassageRecord(){
        return null;
    }
    @Scheduled(fixedRate = 10000) // 每10秒执行一次
    public void refreshCache() {
        cacheAllData();
    }


}
